package com.warren.biz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.warren.annotation.ControllerEndpoint;
import com.warren.biz.entity.InStock;
import com.warren.biz.entity.Product;
import com.warren.biz.entity.Supplier;
import com.warren.biz.service.InStockService;
import com.warren.biz.service.ProductService;
import com.warren.biz.service.SupplierService;
import com.warren.biz.vo.InStockVO;
import com.warren.biz.vo.IntoStockVO;
import com.warren.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.sql.ResultSet;
import java.util.List;



/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author warren
 * @since 2021-04-20
 */
@RestController
@RequestMapping("/biz/inStock")
@Api(value = "物资管理",tags = "物资入库接口")
public class InStockController {

    @Resource
    private InStockService inStockService;

    @Resource
    private SupplierService supplierService;

    @Resource
    private ProductService productService;

    @ApiOperation(value = "分页查询入库列表")
    @PostMapping("/findInStockList")
    public Result findInStockList(@RequestParam(defaultValue = "1")Integer current,
                                  @RequestParam(defaultValue = "6")Integer size,
                                  @RequestBody InStockVO inStockVO){
        Page<InStock> page = new Page<>(current,size);
        QueryWrapper<InStock> queryWrapper = getWrapper(inStockVO);
        IPage<InStock> productPage = inStockService.findInStockList(page,queryWrapper);
        System.out.println(productPage.toString());
        long total = productPage.getTotal();
        List<InStock> records = productPage.getRecords();
        System.out.println(total);
        System.out.println(records);
        return Result.ok().data("total",total).data("records",records);
    }

    @ControllerEndpoint(exceptionMessage = "入库单申请失败",operation = "入库申请")
    @ApiOperation(value = "入库申请")
    @PostMapping("/addIntoStock")
    public Result addIntoStock(@RequestBody IntoStockVO intoStockVO){
        //新增来源
        if (intoStockVO.getSupplierId() == null){
            Supplier supplier = new Supplier();
            supplier.setAddress(intoStockVO.getAddress());
            supplier.setContact(intoStockVO.getAddress());
            supplier.setEmail(intoStockVO.getEmail());
            supplier.setName(intoStockVO.getName());
            supplier.setPhone(intoStockVO.getPhone());
            supplier.setSort(intoStockVO.getSort());
            Supplier supplier1 = supplierService.add(supplier);
            intoStockVO.setSupplierId(supplier1.getId());
        }
        inStockService.addIntoStock(intoStockVO);
        return Result.ok();
    }

    /**
     * 删除入库清单
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除入库清单失败", operation = "删除入库清单")
    @ApiOperation(value = "删除入库清单")
    @DeleteMapping("/delInStock")
    public Result delInStock(Long id,String inNum) {
        inStockService.delete(id,inNum);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "查询物资入库单详细失败",operation = "物资入库详细")
    @ApiOperation(value = "物资入库详细")
    @PostMapping("/detail")
    public Result detail(@RequestParam String inNum,
                         @RequestParam long supplierId,
                         @RequestParam(defaultValue = "1") Integer current,
                         @RequestParam(defaultValue = "3") Integer size){
        //取得分页条件
        Page<Product> page = new Page<>(current,size);
        IPage<Product> productIPage = productService.getDeatis(page,inNum);
        Supplier supplier = supplierService.getById(supplierId);

        long total = productIPage.getTotal();
        List<Product> record = productIPage.getRecords();
        return Result.ok().data("tatol",total).data("records",record).data("supplier",supplier);

    }

    @ControllerEndpoint(exceptionMessage = "入库单回收失败",operation = "入库单回收")
    @ApiOperation(value = "移入回收站",notes = "移入回收站")
    @DeleteMapping("/remove")
    public Result remove(Long id){
        inStockService.remove(id);
        return Result.ok().message("删除成功");
    }

    @ControllerEndpoint(exceptionMessage = "入库单恢复失败",operation = "入库单恢复")
    @ApiOperation(value = "恢复数据",notes = "从回收站中恢复入库单")
    @PutMapping("/back")
    public Result back(Long id){
        inStockService.back(id);
        return Result.ok().message("恢复数据成功");
    }



    private QueryWrapper<InStock> getWrapper(InStockVO inStockVO) {
        QueryWrapper<InStock> queryWrapper = new QueryWrapper<>();
        if (inStockVO != null) {
            //查询每个条件
            if (!ObjectUtils.isEmpty(inStockVO.getStatus())) {
                queryWrapper.eq("status", inStockVO.getStatus());
            }
            if (!ObjectUtils.isEmpty(inStockVO.getInNum())) {
                queryWrapper.like("in_num", inStockVO.getInNum());
            }
            if (!ObjectUtils.isEmpty(inStockVO.getType())) {
                queryWrapper.eq("type",inStockVO.getType());
            }
            if (!ObjectUtils.isEmpty(inStockVO.getStartTime())) {
                queryWrapper.ge("i.create_time",inStockVO.getStartTime());
            }
            if (!ObjectUtils.isEmpty(inStockVO.getEndTime())) {
                queryWrapper.le("i.create_time", inStockVO.getEndTime());
            }
        }
        return queryWrapper;
    }
}

